﻿using LeaRun.Application.Busines.AuthorizeManage;
using LeaRun.Application.Busines.SystemManage;
using LeaRun.Application.Code;
using LeaRun.Application.Entity.AuthorizeManage;
using LeaRun.Application.Entity.SystemManage;
using LeaRun.CodeGenerator;
using LeaRun.CodeGenerator.Model;
using LeaRun.CodeGenerator.Template;
using LeaRun.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace LeaRun.Application.Web.Areas.GeneratorManage.Controllers
{
    /// <summary>
    /// 版 本 6.1
    /// Copyright (c) 2013-2016 上海力软信息技术有限公司
    /// 创建人：佘赐雄
    /// 日 期：2016.2.2 10:29
    /// 描 述：生成器单表
    /// </summary>
    public class ServiceCodeController : MvcControllerBase
    {

        private ModuleBLL moduleBLL = new ModuleBLL();

        [HttpGet]
        public ActionResult CodeBuilder()
		{
            string OutputDirectory = Server.MapPath("~/Web.config"); ;
            for (int i = 0; i < 2; i++)
                OutputDirectory = OutputDirectory.Substring(0, OutputDirectory.LastIndexOf('\\'));
            ViewBag.OutputDirectory = OutputDirectory;
            ViewBag.UserName = OperatorProvider.Provider.Current().UserName;
            return View();
		}
        [AjaxOnly(false), HttpPost]
        public ActionResult LookCode(BaseConfigModel r)
        {
            ServiceClass default_Template = new ServiceClass();
            IEnumerable<DataBaseTableFieldEntity> tableFiledList = new DataBaseTableBLL().GetTableFiledList(r.DataBaseLinkId, r.DataBaseTableName);
            string entityCode = default_Template.EntityBuilder(r, DataHelper.ListToDataTable<DataBaseTableFieldEntity>(tableFiledList.ToList<DataBaseTableFieldEntity>()));
            string entitymapCode = default_Template.EntityMapBuilder(r);
            string serviceCode = default_Template.ServiceBuilder(r);
            string iserviceCode = default_Template.IServiceBuilder(r);
            string businesCode = default_Template.BusinesBuilder(r);
            var data = new
            {
                entityCode,
                entitymapCode,
                serviceCode,
                iserviceCode,
                businesCode
            };
            return this.ToJsonResult(data);
        }
        [AjaxOnly(false), HttpPost]
        public ActionResult CreateCode(BaseConfigModel hi, string strCode)
        {
            CreateCodeFile.CreateServiceExecution(hi, base.Server.UrlDecode(strCode));
            return this.Success("恭喜您，创建成功！");
        }
    }
}
